package ph.com.gs3.formalistics.presenter.fragments.views;

import java.util.List;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.model.valueobjects.business.Comment;
import ph.com.gs3.formalistics.presenter.adapters.CommentListItemViewAdapter;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

/**
 * 
 * @author Roni Pinili
 */

public class CommentsViewFragment extends Fragment {

	private ListView lvCommentsContainer;
	private EditText etCommentText;
	private Button bSubmitComment;

	private List<Comment> commentsOnDisplay;
	private CommentListItemViewAdapter commentListItemViewAdapter;
	private CommentListViewActionListener listener;

	public static CommentsViewFragment createInstance(Context context,
	        List<Comment> commentsOnDisplay) {

		CommentsViewFragment instance = new CommentsViewFragment();
		instance.commentsOnDisplay = commentsOnDisplay;
		instance.commentListItemViewAdapter = new CommentListItemViewAdapter(context,
		        instance.commentsOnDisplay);

		return instance;

	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		try {
			listener = (CommentListViewActionListener) activity;
		} catch (ClassCastException e) {
			throw new IllegalStateException(activity.getClass().getSimpleName()
			        + " must implement " + CommentsViewFragment.class.getSimpleName());
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.fragment_comment, container, false);

		etCommentText = (EditText) rootView.findViewById(R.id.Comment_etCommentText);

		bSubmitComment = (Button) rootView.findViewById(R.id.Comment_bSubmitComment);
		bSubmitComment.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				submitNewComment();
			}
		});

		lvCommentsContainer = (ListView) rootView.findViewById(R.id.Comment_lvCommentsContainer);
		lvCommentsContainer.setAdapter(commentListItemViewAdapter);
		lvCommentsContainer.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// Comment clickedComment = (Comment)
				// commentListItemViewAdapter.getItem(position);
				// if (etCommentText.getText().toString().isEmpty()) {
				// // etCommentText.setFocusableInTouchMode(true);
				// // etCommentText.requestFocus();
				//
				// etCommentText.setText("To " + clickedComment.getAuthorDisplayName() +
				// ": ");
				// }
			}

		});

		getActivity().registerForContextMenu(lvCommentsContainer);

		scrollToBottom();

		return rootView;
	}

	public void markCommentAsProcessed(String commentDbId) {

		int commentCount = commentsOnDisplay.size();

		// TODO: replace this with search for pending comment only, not the whole comment
		// list
		for (int i = 0; i < commentCount; i++) {
			if (commentDbId.equals(commentsOnDisplay.get(i).getDbId())) {
				commentsOnDisplay.get(i).setCurrentlyBeingProcessed(false);
				commentsOnDisplay.get(i).setOutgoing(false);
				break;
			}
		}

		commentListItemViewAdapter.notifyDataSetChanged();

	}

	public void addComment(Comment comment) {
		commentsOnDisplay.add(comment);
		commentListItemViewAdapter.notifyDataSetChanged();
	}
	
	public void removeComment(Comment comment) {
		commentsOnDisplay.remove(comment);
		commentListItemViewAdapter.notifyDataSetChanged();
	}

	public void scrollToBottom() {
		lvCommentsContainer.setSelection(commentListItemViewAdapter.getCount());
	}

	public void submitNewComment() {

		String commentText = etCommentText.getText().toString();

		if (!commentText.isEmpty()) {
			listener.onSubmitCommentCommand(commentText);
			scrollToBottom();
		}

		etCommentText.setText("");
	}

	public static interface CommentListViewActionListener {
		public void onSubmitCommentCommand(String commentText);
	}

}
